﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Global.Class.Library;

namespace DataAccessLayer
{
    public class CommonDatabaseView
    {
        public CommonDatabaseView()
        {
            try
            {
                if (GlobalVariables.shouldRestoreProcedure) RestoreProcedure();
            }
            catch (Exception exception)
            {
                throw exception;
            }

        }

        private static void RestoreProcedure()
        {
            CommonDatabaseView.RestoreVWListCustomerNameColumn();
            CommonDatabaseView.RestoreVWListItemCategory();
            CommonDatabaseView.RestoreVWListItemType();
            CommonDatabaseView.RestoreVWListItemClass();
            CommonDatabaseView.RestoreVWListItemBrand();
            CommonDatabaseView.RestoreVWListItemPM_APM();
            CommonDatabaseView.RestoreVWListCustomerCategory();
            CommonDatabaseView.RestoreVWListCustomerType();
            CommonDatabaseView.RestoreVWListCustomerChannel();
            CommonDatabaseView.RestoreVWListCustomerGroup();
            CommonDatabaseView.RestoreVWListSalesmenItemClass();
            CommonDatabaseView.RestoreVWListAddressArea();
            CommonDatabaseView.RestoreVWListCustomerName();
            CommonDatabaseView.RestoreVWListSalesmenCustomer();
            CommonDatabaseView.RestoreVWListSalesmenCategory();
            CommonDatabaseView.RestoreVWListDistributorCategory();
            CommonDatabaseView.RestoreVWListStaffCategory();
        }

        private static void RestoreVWListCustomerNameColumn()
        {
            string queryString = "";
            queryString = "         SELECT      CustomerID, MAX(CASE WHEN DistributorID = 88 THEN ColumnValue ELSE '' END) AS DescriptionZuellig, MAX(CASE WHEN DistributorID = 82 THEN ColumnValue ELSE '' END) AS DescriptionDiethelm, " + "\r\n";
            queryString = queryString + "               SUM(CASE WHEN DistributorID = 88 THEN 1 ELSE 0 END) AS CountZuellig, SUM(CASE WHEN DistributorID = 82 THEN 1 ELSE 0 END) AS CountDiethelm " + "\r\n";
            queryString = queryString + "   FROM        ListCustomerNameColumn " + "\r\n";
            queryString = queryString + "   GROUP BY    CustomerID" + "\r\n";


            SQLDatabase.CreateView("VWListCustomerNameColumn", queryString);
        }


        private static void RestoreVWListItemCategory()
        {
            string queryString = "";

            //---************************VWListItemCategory.BEGIN

            //--------A1.BIGIN
            queryString = "                 SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategoryID, Description, " + "\r\n";

            queryString = queryString + "               ItemCategoryID AS ItemCategoryID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListItemCategory WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END

            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory2.ItemCategoryID, ItemCategory2.Description, " + "\r\n";

            queryString = queryString + "               ItemCategory1.ItemCategoryID AS ItemCategoryID1, ItemCategory1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.ItemCategoryID AS ItemCategoryID2, ItemCategory2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.ItemCategoryID = ItemCategory2.AncestorID " + "\r\n";
            //--------A2.END

            //--------A3.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory3.ItemCategoryID, ItemCategory3.Description, " + "\r\n";

            queryString = queryString + "               ItemCategory1.ItemCategoryID AS ItemCategoryID1, ItemCategory1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.ItemCategoryID AS ItemCategoryID2, ItemCategory2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               ItemCategory3.ItemCategoryID AS ItemCategoryID3, ItemCategory3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.ItemCategoryID = ItemCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 3)) AS ItemCategory3 " + "\r\n";
            queryString = queryString + "               ON ItemCategory2.ItemCategoryID = ItemCategory3.AncestorID " + "\r\n";
            //--------A3.END


            //--------A4.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory4.ItemCategoryID, ItemCategory4.Description, " + "\r\n";

            queryString = queryString + "               ItemCategory1.ItemCategoryID AS ItemCategoryID1, ItemCategory1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.ItemCategoryID AS ItemCategoryID2, ItemCategory2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               ItemCategory3.ItemCategoryID AS ItemCategoryID3, ItemCategory3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               ItemCategory4.ItemCategoryID AS ItemCategoryID4, ItemCategory4.Description AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.ItemCategoryID = ItemCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 3)) AS ItemCategory3 " + "\r\n";
            queryString = queryString + "               ON ItemCategory2.ItemCategoryID = ItemCategory3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemCategoryID, Description, AncestorID FROM ListItemCategory WHERE (HierarchyLevel = 4)) AS ItemCategory4 " + "\r\n";
            queryString = queryString + "               ON ItemCategory3.ItemCategoryID = ItemCategory4.AncestorID " + "\r\n";
            //--------A4.END

            SQLDatabase.CreateView("VWListItemCategory", queryString);

            //---************************VWListItemCategory.END
        }


        private static void RestoreVWListItemType()
        {
            string queryString = "";

            //---************************VWListItemType.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemTypeID, Description, " + "\r\n";

            queryString = queryString + "               ItemTypeID AS ItemTypeID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListItemType WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemType2.ItemTypeID, ItemType2.Description, " + "\r\n";

            queryString = queryString + "               ItemType1.ItemTypeID AS ItemTypeID1, ItemType1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemType2.ItemTypeID AS ItemTypeID2, ItemType2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemTypeID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemTypeID, Description,  AncestorID FROM ListItemType WHERE (HierarchyLevel = 1)) AS ItemType1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemTypeID, Description,  AncestorID FROM ListItemType WHERE (HierarchyLevel = 2)) AS ItemType2 " + "\r\n";
            queryString = queryString + "               ON ItemType1.ItemTypeID = ItemType2.AncestorID " + "\r\n";
            //--------A2.END


            SQLDatabase.CreateView("VWListItemType", queryString);

            //---************************VWListItemType.END
        }


        private static void RestoreVWListItemClass()
        {
            string queryString = "";
            //---************************VWListItemClass.BEGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemClassID, Description, " + "\r\n";

            queryString = queryString + "               ItemClassID AS ItemClassID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListItemClass WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemClass2.ItemClassID, ItemClass2.Description, " + "\r\n";

            queryString = queryString + "               ItemClass1.ItemClassID AS ItemClassID1, ItemClass1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemClass2.ItemClassID AS ItemClassID2, ItemClass2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 1)) AS ItemClass1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 2)) AS ItemClass2 " + "\r\n";
            queryString = queryString + "               ON ItemClass1.ItemClassID = ItemClass2.AncestorID " + "\r\n";
            //--------A2.END

            //--------A3.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemClass3.ItemClassID, ItemClass3.Description, " + "\r\n";

            queryString = queryString + "               ItemClass1.ItemClassID AS ItemClassID1, ItemClass1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemClass2.ItemClassID AS ItemClassID2, ItemClass2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               ItemClass3.ItemClassID AS ItemClassID3, ItemClass3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 1)) AS ItemClass1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 2)) AS ItemClass2 " + "\r\n";
            queryString = queryString + "               ON ItemClass1.ItemClassID = ItemClass2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 3)) AS ItemClass3 " + "\r\n";
            queryString = queryString + "               ON ItemClass2.ItemClassID = ItemClass3.AncestorID " + "\r\n";
            //--------A3.END


            //--------A4.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemClass4.ItemClassID, ItemClass4.Description, " + "\r\n";

            queryString = queryString + "               ItemClass1.ItemClassID AS ItemClassID1, ItemClass1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemClass2.ItemClassID AS ItemClassID2, ItemClass2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               ItemClass3.ItemClassID AS ItemClassID3, ItemClass3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               ItemClass4.ItemClassID AS ItemClassID4, ItemClass4.Description AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemClassID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 1)) AS ItemClass1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 2)) AS ItemClass2 " + "\r\n";
            queryString = queryString + "               ON ItemClass1.ItemClassID = ItemClass2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 3)) AS ItemClass3 " + "\r\n";
            queryString = queryString + "               ON ItemClass2.ItemClassID = ItemClass3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemClassID, Description,  AncestorID FROM ListItemClass WHERE (HierarchyLevel = 4)) AS ItemClass4 " + "\r\n";
            queryString = queryString + "               ON ItemClass3.ItemClassID = ItemClass4.AncestorID " + "\r\n";
            //--------A4.END

            SQLDatabase.CreateView("VWListItemClass", queryString);
            //---************************VWListItemClass.END
        }


        private static void RestoreVWListItemBrand()
        {
            string queryString = "";

            //---************************VWListItemBrand.BEGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemBrandID, Description, " + "\r\n";

            queryString = queryString + "               ItemBrandID AS ItemBrandID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListItemBrand WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemBrand2.ItemBrandID, ItemBrand2.Description, " + "\r\n";

            queryString = queryString + "               ItemBrand1.ItemBrandID AS ItemBrandID1, ItemBrand1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemBrand2.ItemBrandID AS ItemBrandID2, ItemBrand2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemBrandID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemBrandID, Description,  AncestorID FROM ListItemBrand WHERE (HierarchyLevel = 1)) AS ItemBrand1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemBrandID, Description,  AncestorID FROM ListItemBrand WHERE (HierarchyLevel = 2)) AS ItemBrand2 " + "\r\n";
            queryString = queryString + "               ON ItemBrand1.ItemBrandID = ItemBrand2.AncestorID " + "\r\n";
            //--------A2.END

            SQLDatabase.CreateView("VWListItemBrand", queryString);
            //---************************VWListItemBrand.END
        }


        private static void RestoreVWListItemPM_APM()
        {
            string queryString = "";
            //---************************VWListItemPM_APM.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemPM_APMID, Description, " + "\r\n";

            queryString = queryString + "               ItemPM_APMID AS ItemPM_APMID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListItemPM_APM WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemPM_APM2.ItemPM_APMID, ItemPM_APM2.Description, " + "\r\n";

            queryString = queryString + "               ItemPM_APM1.ItemPM_APMID AS ItemPM_APMID1, ItemPM_APM1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               ItemPM_APM2.ItemPM_APMID AS ItemPM_APMID2, ItemPM_APM2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS ItemPM_APMID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     ItemPM_APMID, Description,  AncestorID FROM ListItemPM_APM WHERE (HierarchyLevel = 1)) AS ItemPM_APM1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     ItemPM_APMID, Description,  AncestorID FROM ListItemPM_APM WHERE (HierarchyLevel = 2)) AS ItemPM_APM2 " + "\r\n";
            queryString = queryString + "               ON ItemPM_APM1.ItemPM_APMID = ItemPM_APM2.AncestorID " + "\r\n";


            SQLDatabase.CreateView("VWListItemPM_APM", queryString);

            //---************************VWListItemPM_APM.END
        }


        private static void RestoreVWListCustomerCategory()
        {
            string queryString = "";
            //---************************VWListCustomerCategory.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerCategoryID, Description, " + "\r\n";

            queryString = queryString + "               CustomerCategoryID AS CustomerCategoryID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS CustomerCategoryID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS CustomerCategoryID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListCustomerCategory WHERE (HierarchyLevel = 1) " + "\r\n";

            SQLDatabase.CreateView("VWListCustomerCategory", queryString);
            //---************************VWListCustomerCategory.END
        }


        private static void RestoreVWListCustomerType()
        {
            string queryString = "";
            //---************************VWListCustomerType.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerTypeID, Description, " + "\r\n";

            queryString = queryString + "               CustomerTypeID AS CustomerTypeID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListCustomerType WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerType2.CustomerTypeID, CustomerType2.Description, " + "\r\n";

            queryString = queryString + "               CustomerType1.CustomerTypeID AS CustomerTypeID1, CustomerType1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerType2.CustomerTypeID AS CustomerTypeID2, CustomerType2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 1)) AS CustomerType1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 2)) AS CustomerType2 " + "\r\n";
            queryString = queryString + "               ON CustomerType1.CustomerTypeID = CustomerType2.AncestorID " + "\r\n";
            //--------A2.END

            //--------A3.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerType3.CustomerTypeID, CustomerType3.Description, " + "\r\n";

            queryString = queryString + "               CustomerType1.CustomerTypeID AS CustomerTypeID1, CustomerType1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerType2.CustomerTypeID AS CustomerTypeID2, CustomerType2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               CustomerType3.CustomerTypeID AS CustomerTypeID3, CustomerType3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 1)) AS CustomerType1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 2)) AS CustomerType2 " + "\r\n";
            queryString = queryString + "               ON CustomerType1.CustomerTypeID = CustomerType2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 3)) AS CustomerType3 " + "\r\n";
            queryString = queryString + "               ON CustomerType2.CustomerTypeID = CustomerType3.AncestorID " + "\r\n";
            //--------A3.END


            //--------A4.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerType4.CustomerTypeID, CustomerType4.Description, " + "\r\n";

            queryString = queryString + "               CustomerType1.CustomerTypeID AS CustomerTypeID1, CustomerType1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerType2.CustomerTypeID AS CustomerTypeID2, CustomerType2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               CustomerType3.CustomerTypeID AS CustomerTypeID3, CustomerType3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               CustomerType4.CustomerTypeID AS CustomerTypeID4, CustomerType4.Description AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerTypeID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 1)) AS CustomerType1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 2)) AS CustomerType2 " + "\r\n";
            queryString = queryString + "               ON CustomerType1.CustomerTypeID = CustomerType2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 3)) AS CustomerType3 " + "\r\n";
            queryString = queryString + "               ON CustomerType2.CustomerTypeID = CustomerType3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerTypeID, Description,  AncestorID FROM ListCustomerType WHERE (HierarchyLevel = 4)) AS CustomerType4 " + "\r\n";
            queryString = queryString + "               ON CustomerType3.CustomerTypeID = CustomerType4.AncestorID " + "\r\n";
            //--------A4.END

            SQLDatabase.CreateView("VWListCustomerType", queryString);

            //---************************VWListCustomerType.END
        }


        private static void RestoreVWListCustomerChannel()
        {
            string queryString = "";
            //---************************VWListCustomerChannel.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerChannelID, Description, " + "\r\n";

            queryString = queryString + "               CustomerChannelID AS CustomerChannelID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListCustomerChannel WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerChannel2.CustomerChannelID, CustomerChannel2.Description, " + "\r\n";

            queryString = queryString + "               CustomerChannel1.CustomerChannelID AS CustomerChannelID1, CustomerChannel1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerChannel2.CustomerChannelID AS CustomerChannelID2, CustomerChannel2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 1)) AS CustomerChannel1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 2)) AS CustomerChannel2 " + "\r\n";
            queryString = queryString + "               ON CustomerChannel1.CustomerChannelID = CustomerChannel2.AncestorID " + "\r\n";
            //--------A2.END

            //--------A3.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerChannel3.CustomerChannelID, CustomerChannel3.Description, " + "\r\n";

            queryString = queryString + "               CustomerChannel1.CustomerChannelID AS CustomerChannelID1, CustomerChannel1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerChannel2.CustomerChannelID AS CustomerChannelID2, CustomerChannel2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               CustomerChannel3.CustomerChannelID AS CustomerChannelID3, CustomerChannel3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 1)) AS CustomerChannel1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 2)) AS CustomerChannel2 " + "\r\n";
            queryString = queryString + "               ON CustomerChannel1.CustomerChannelID = CustomerChannel2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 3)) AS CustomerChannel3 " + "\r\n";
            queryString = queryString + "               ON CustomerChannel2.CustomerChannelID = CustomerChannel3.AncestorID " + "\r\n";
            //--------A3.END


            //--------A4.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerChannel4.CustomerChannelID, CustomerChannel4.Description, " + "\r\n";

            queryString = queryString + "               CustomerChannel1.CustomerChannelID AS CustomerChannelID1, CustomerChannel1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerChannel2.CustomerChannelID AS CustomerChannelID2, CustomerChannel2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               CustomerChannel3.CustomerChannelID AS CustomerChannelID3, CustomerChannel3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               CustomerChannel4.CustomerChannelID AS CustomerChannelID4, CustomerChannel4.Description AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerChannelID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 1)) AS CustomerChannel1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 2)) AS CustomerChannel2 " + "\r\n";
            queryString = queryString + "               ON CustomerChannel1.CustomerChannelID = CustomerChannel2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 3)) AS CustomerChannel3 " + "\r\n";
            queryString = queryString + "               ON CustomerChannel2.CustomerChannelID = CustomerChannel3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerChannelID, Description,  AncestorID FROM ListCustomerChannel WHERE (HierarchyLevel = 4)) AS CustomerChannel4 " + "\r\n";
            queryString = queryString + "               ON CustomerChannel3.CustomerChannelID = CustomerChannel4.AncestorID " + "\r\n";
            //--------A4.END

            SQLDatabase.CreateView("VWListCustomerChannel", queryString);

            //---************************VWListCustomerChannel.END
        }


        private static void RestoreVWListCustomerGroup()
        {
            string queryString = "";
            //---************************VWListCustomerGroup.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerGroupID, Description, " + "\r\n";

            queryString = queryString + "               CustomerGroupID AS CustomerGroupID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListCustomerGroup WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerGroup2.CustomerGroupID, CustomerGroup2.Description, " + "\r\n";

            queryString = queryString + "               CustomerGroup1.CustomerGroupID AS CustomerGroupID1, CustomerGroup1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerGroup2.CustomerGroupID AS CustomerGroupID2, CustomerGroup2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 1)) AS CustomerGroup1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 2)) AS CustomerGroup2 " + "\r\n";
            queryString = queryString + "               ON CustomerGroup1.CustomerGroupID = CustomerGroup2.AncestorID " + "\r\n";
            //--------A2.END

            //--------A3.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerGroup3.CustomerGroupID, CustomerGroup3.Description, " + "\r\n";

            queryString = queryString + "               CustomerGroup1.CustomerGroupID AS CustomerGroupID1, CustomerGroup1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerGroup2.CustomerGroupID AS CustomerGroupID2, CustomerGroup2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               CustomerGroup3.CustomerGroupID AS CustomerGroupID3, CustomerGroup3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 1)) AS CustomerGroup1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 2)) AS CustomerGroup2 " + "\r\n";
            queryString = queryString + "               ON CustomerGroup1.CustomerGroupID = CustomerGroup2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 3)) AS CustomerGroup3 " + "\r\n";
            queryString = queryString + "               ON CustomerGroup2.CustomerGroupID = CustomerGroup3.AncestorID " + "\r\n";
            //--------A3.END


            //--------A4.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               CustomerGroup4.CustomerGroupID, CustomerGroup4.Description, " + "\r\n";

            queryString = queryString + "               CustomerGroup1.CustomerGroupID AS CustomerGroupID1, CustomerGroup1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               CustomerGroup2.CustomerGroupID AS CustomerGroupID2, CustomerGroup2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               CustomerGroup3.CustomerGroupID AS CustomerGroupID3, CustomerGroup3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               CustomerGroup4.CustomerGroupID AS CustomerGroupID4, CustomerGroup4.Description AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS CustomerGroupID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 1)) AS CustomerGroup1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 2)) AS CustomerGroup2 " + "\r\n";
            queryString = queryString + "               ON CustomerGroup1.CustomerGroupID = CustomerGroup2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 3)) AS CustomerGroup3 " + "\r\n";
            queryString = queryString + "               ON CustomerGroup2.CustomerGroupID = CustomerGroup3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     CustomerGroupID, Description,  AncestorID FROM ListCustomerGroup WHERE (HierarchyLevel = 4)) AS CustomerGroup4 " + "\r\n";
            queryString = queryString + "               ON CustomerGroup3.CustomerGroupID = CustomerGroup4.AncestorID " + "\r\n";
            //--------A4.END      

            SQLDatabase.CreateView("VWListCustomerGroup", queryString);
            //---************************VWListCustomerGroup.END
        }


        private static void RestoreVWListSalesmenItemClass()
        {
            string queryString = "";
            //---************************VWListSalesmenItemClass.BEGIN
            queryString = "         SELECT      SalesmenID, MAX(ItemClassCC) AS ItemClassCC, MAX(ItemClassNonCC) AS ItemClassNonCC " + "\r\n";
            queryString = queryString + "   FROM       (SELECT      ListSalesmenItemClass.SalesmenID, " + "\r\n";
            queryString = queryString + "                           CASE ListSalesmenItemClass.ItemClassID WHEN 1 THEN ListItemClass.Description ELSE '' END AS ItemClassCC, " + "\r\n";
            queryString = queryString + "                           CASE ListSalesmenItemClass.ItemClassID WHEN 2 THEN ListItemClass.Description ELSE '' END AS ItemClassNonCC " + "\r\n";
            queryString = queryString + "               FROM        ListSalesmenItemClass INNER JOIN" + "\r\n";
            queryString = queryString + "                           ListItemClass ON ListSalesmenItemClass.ItemClassID = ListItemClass.ItemClassID) DERIVEDTBL " + "\r\n";
            queryString = queryString + "   GROUP BY SalesmenID" + "\r\n";

            SQLDatabase.CreateView("VWListSalesmenItemClass", queryString);
            //---************************VWListSalesmenItemClass.BEGIN
        }


        private static void RestoreVWListAddressArea()
        {
            string queryString = "";
            //---************************VWListAddressArea.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               AddressAreaID, Description, AreaCode, " + "\r\n";

            queryString = queryString + "               AddressAreaID AS AddressAreaID1, Description AS Description1, AreaCode AS AreaCode1, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID2, '' AS Description2, '' AS AreaCode2, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID3, '' AS Description3, '' AS AreaCode3, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID4, '' AS Description4, '' AS AreaCode4, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID5, '' AS Description5, '' AS AreaCode5, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID6, '' AS Description6, '' AS AreaCode6, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID7, '' AS Description7, '' AS AreaCode7, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID8, '' AS Description8, '' AS AreaCode8, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID9, '' AS Description9, '' AS AreaCode9  " + "\r\n";
            queryString = queryString + "               , Leafest, AdministratorLevelID " + "\r\n";

            queryString = queryString + "   FROM        ListAddressArea WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory2.AddressAreaID, ItemCategory2.Description, ItemCategory2.AreaCode, " + "\r\n";

            queryString = queryString + "               ItemCategory1.AddressAreaID AS AddressAreaID1, ItemCategory1.Description AS Description1, ItemCategory1.AreaCode AS AreaCode1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.AddressAreaID AS AddressAreaID2, ItemCategory2.Description AS Description2, ItemCategory2.AreaCode AS AreaCode2, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID3, '' AS Description3, '' AS AreaCode3, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID4, '' AS Description4, '' AS AreaCode4, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID5, '' AS Description5, '' AS AreaCode5, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID6, '' AS Description6, '' AS AreaCode6, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID7, '' AS Description7, '' AS AreaCode7, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID8, '' AS Description8, '' AS AreaCode8, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID9, '' AS Description9, '' AS AreaCode9 " + "\r\n";
            queryString = queryString + "               , ItemCategory2.Leafest, ItemCategory2.AdministratorLevelID " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.AddressAreaID = ItemCategory2.AncestorID " + "\r\n";
            //--------A2.END

            //--------A3.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory3.AddressAreaID, ItemCategory3.Description, ItemCategory3.AreaCode, " + "\r\n";

            queryString = queryString + "               ItemCategory1.AddressAreaID AS AddressAreaID1, ItemCategory1.Description AS Description1, ItemCategory1.AreaCode AS AreaCode1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.AddressAreaID AS AddressAreaID2, ItemCategory2.Description AS Description2, ItemCategory2.AreaCode AS AreaCode2, " + "\r\n";
            queryString = queryString + "               ItemCategory3.AddressAreaID AS AddressAreaID3, ItemCategory3.Description AS Description3, ItemCategory3.AreaCode AS AreaCode3, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID4, '' AS Description4, '' AS AreaCode4, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID5, '' AS Description5, '' AS AreaCode5, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID6, '' AS Description6, '' AS AreaCode6, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID7, '' AS Description7, '' AS AreaCode7, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID8, '' AS Description8, '' AS AreaCode8, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID9, '' AS Description9, '' AS AreaCode9 " + "\r\n";
            queryString = queryString + "               , ItemCategory3.Leafest, ItemCategory3.AdministratorLevelID " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.AddressAreaID = ItemCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 3)) AS ItemCategory3 " + "\r\n";
            queryString = queryString + "               ON ItemCategory2.AddressAreaID = ItemCategory3.AncestorID " + "\r\n";
            //--------A3.END


            //--------A4.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory4.AddressAreaID, ItemCategory4.Description, ItemCategory4.AreaCode, " + "\r\n";

            queryString = queryString + "               ItemCategory1.AddressAreaID AS AddressAreaID1, ItemCategory1.Description AS Description1, ItemCategory1.AreaCode AS AreaCode1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.AddressAreaID AS AddressAreaID2, ItemCategory2.Description AS Description2, ItemCategory2.AreaCode AS AreaCode2, " + "\r\n";
            queryString = queryString + "               ItemCategory3.AddressAreaID AS AddressAreaID3, ItemCategory3.Description AS Description3, ItemCategory3.AreaCode AS AreaCode3, " + "\r\n";
            queryString = queryString + "               ItemCategory4.AddressAreaID AS AddressAreaID4, ItemCategory4.Description AS Description4, ItemCategory4.AreaCode AS AreaCode4, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID5, '' AS Description5, '' AS AreaCode5, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID6, '' AS Description6, '' AS AreaCode6, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID7, '' AS Description7, '' AS AreaCode7, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID8, '' AS Description8, '' AS AreaCode8, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID9, '' AS Description9, '' AS AreaCode9 " + "\r\n";
            queryString = queryString + "               , ItemCategory4.Leafest, ItemCategory4.AdministratorLevelID " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.AddressAreaID = ItemCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 3)) AS ItemCategory3 " + "\r\n";
            queryString = queryString + "               ON ItemCategory2.AddressAreaID = ItemCategory3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 4)) AS ItemCategory4 " + "\r\n";
            queryString = queryString + "               ON ItemCategory3.AddressAreaID = ItemCategory4.AncestorID " + "\r\n";
            //--------A4.END


            //--------A5.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory5.AddressAreaID, ItemCategory5.Description, ItemCategory5.AreaCode, " + "\r\n";

            queryString = queryString + "               ItemCategory1.AddressAreaID AS AddressAreaID1, ItemCategory1.Description AS Description1, ItemCategory1.AreaCode AS AreaCode1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.AddressAreaID AS AddressAreaID2, ItemCategory2.Description AS Description2, ItemCategory2.AreaCode AS AreaCode2, " + "\r\n";
            queryString = queryString + "               ItemCategory3.AddressAreaID AS AddressAreaID3, ItemCategory3.Description AS Description3, ItemCategory3.AreaCode AS AreaCode3, " + "\r\n";
            queryString = queryString + "               ItemCategory4.AddressAreaID AS AddressAreaID4, ItemCategory4.Description AS Description4, ItemCategory4.AreaCode AS AreaCode4, " + "\r\n";
            queryString = queryString + "               ItemCategory5.AddressAreaID AS AddressAreaID5, ItemCategory5.Description AS Description5, ItemCategory5.AreaCode AS AreaCode5, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID6, '' AS Description6, '' AS AreaCode6, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID7, '' AS Description7, '' AS AreaCode7, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID8, '' AS Description8, '' AS AreaCode8, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID9, '' AS Description9, '' AS AreaCode9 " + "\r\n";
            queryString = queryString + "               , ItemCategory5.Leafest, ItemCategory5.AdministratorLevelID " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.AddressAreaID = ItemCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 3)) AS ItemCategory3 " + "\r\n";
            queryString = queryString + "               ON ItemCategory2.AddressAreaID = ItemCategory3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 4)) AS ItemCategory4 " + "\r\n";
            queryString = queryString + "               ON ItemCategory3.AddressAreaID = ItemCategory4.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 5)) AS ItemCategory5 " + "\r\n";
            queryString = queryString + "               ON ItemCategory4.AddressAreaID = ItemCategory5.AncestorID " + "\r\n";
            //--------A5.END


            //--------A6.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory6.AddressAreaID, ItemCategory6.Description, ItemCategory6.AreaCode, " + "\r\n";

            queryString = queryString + "               ItemCategory1.AddressAreaID AS AddressAreaID1, ItemCategory1.Description AS Description1, ItemCategory1.AreaCode AS AreaCode1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.AddressAreaID AS AddressAreaID2, ItemCategory2.Description AS Description2, ItemCategory2.AreaCode AS AreaCode2, " + "\r\n";
            queryString = queryString + "               ItemCategory3.AddressAreaID AS AddressAreaID3, ItemCategory3.Description AS Description3, ItemCategory3.AreaCode AS AreaCode3, " + "\r\n";
            queryString = queryString + "               ItemCategory4.AddressAreaID AS AddressAreaID4, ItemCategory4.Description AS Description4, ItemCategory4.AreaCode AS AreaCode4, " + "\r\n";
            queryString = queryString + "               ItemCategory5.AddressAreaID AS AddressAreaID5, ItemCategory5.Description AS Description5, ItemCategory5.AreaCode AS AreaCode5, " + "\r\n";
            queryString = queryString + "               ItemCategory6.AddressAreaID AS AddressAreaID6, ItemCategory6.Description AS Description6, ItemCategory6.AreaCode AS AreaCode6, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID7, '' AS Description7, '' AS AreaCode7, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID8, '' AS Description8, '' AS AreaCode8, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID9, '' AS Description9, '' AS AreaCode9 " + "\r\n";
            queryString = queryString + "               , ItemCategory6.Leafest, ItemCategory6.AdministratorLevelID " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.AddressAreaID = ItemCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 3)) AS ItemCategory3 " + "\r\n";
            queryString = queryString + "               ON ItemCategory2.AddressAreaID = ItemCategory3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 4)) AS ItemCategory4 " + "\r\n";
            queryString = queryString + "               ON ItemCategory3.AddressAreaID = ItemCategory4.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 5)) AS ItemCategory5 " + "\r\n";
            queryString = queryString + "               ON ItemCategory4.AddressAreaID = ItemCategory5.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 6)) AS ItemCategory6 " + "\r\n";
            queryString = queryString + "               ON ItemCategory5.AddressAreaID = ItemCategory6.AncestorID " + "\r\n";
            //--------A6.END


            //--------A7.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               ItemCategory7.AddressAreaID, ItemCategory7.Description, ItemCategory7.AreaCode, " + "\r\n";

            queryString = queryString + "               ItemCategory1.AddressAreaID AS AddressAreaID1, ItemCategory1.Description AS Description1, ItemCategory1.AreaCode AS AreaCode1, " + "\r\n";
            queryString = queryString + "               ItemCategory2.AddressAreaID AS AddressAreaID2, ItemCategory2.Description AS Description2, ItemCategory2.AreaCode AS AreaCode2, " + "\r\n";
            queryString = queryString + "               ItemCategory3.AddressAreaID AS AddressAreaID3, ItemCategory3.Description AS Description3, ItemCategory3.AreaCode AS AreaCode3, " + "\r\n";
            queryString = queryString + "               ItemCategory4.AddressAreaID AS AddressAreaID4, ItemCategory4.Description AS Description4, ItemCategory4.AreaCode AS AreaCode4, " + "\r\n";
            queryString = queryString + "               ItemCategory5.AddressAreaID AS AddressAreaID5, ItemCategory5.Description AS Description5, ItemCategory5.AreaCode AS AreaCode5, " + "\r\n";
            queryString = queryString + "               ItemCategory6.AddressAreaID AS AddressAreaID6, ItemCategory6.Description AS Description6, ItemCategory6.AreaCode AS AreaCode6, " + "\r\n";
            queryString = queryString + "               ItemCategory7.AddressAreaID AS AddressAreaID7, ItemCategory7.Description AS Description7, ItemCategory7.AreaCode AS AreaCode7, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID8, '' AS Description8, '' AS AreaCode8, " + "\r\n";
            queryString = queryString + "               0 AS AddressAreaID9, '' AS Description9, '' AS AreaCode9  " + "\r\n";
            queryString = queryString + "               , ItemCategory7.Leafest, ItemCategory7.AdministratorLevelID " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 1)) AS ItemCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 2)) AS ItemCategory2 " + "\r\n";
            queryString = queryString + "               ON ItemCategory1.AddressAreaID = ItemCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 3)) AS ItemCategory3 " + "\r\n";
            queryString = queryString + "               ON ItemCategory2.AddressAreaID = ItemCategory3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 4)) AS ItemCategory4 " + "\r\n";
            queryString = queryString + "               ON ItemCategory3.AddressAreaID = ItemCategory4.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 5)) AS ItemCategory5 " + "\r\n";
            queryString = queryString + "               ON ItemCategory4.AddressAreaID = ItemCategory5.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 6)) AS ItemCategory6 " + "\r\n";
            queryString = queryString + "               ON ItemCategory5.AddressAreaID = ItemCategory6.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     AddressAreaID, Description, AreaCode, Leafest, AdministratorLevelID,  AncestorID FROM ListAddressArea WHERE (HierarchyLevel = 7)) AS ItemCategory7 " + "\r\n";
            queryString = queryString + "               ON ItemCategory6.AddressAreaID = ItemCategory7.AncestorID " + "\r\n";
            //--------A7.END       

            SQLDatabase.CreateView("VWListAddressArea", queryString);
            //---************************VWListAddressArea.END
        }


        private static void RestoreVWListCustomerName()
        {
            //---************************VWListCustomerName.BEGIN
            string queryString = "";
            queryString = "         SELECT      ListCustomerName.CustomerID, ListCustomerName.Description, ListCustomerName.DescriptionOfficial, " + "\r\n";
            queryString = queryString + "               ListCustomerName.DescriptionOriginal, ListCustomerName.VATCode, ListCustomerName.ContactName, " + "\r\n";
            queryString = queryString + "               ListCustomerName.AttentionName, ListCustomerName.Telephone, ListCustomerName.EMailAddress, ListCustomerName.Other, " + "\r\n";
            queryString = queryString + "               ListCustomerName.DependentOnly, ListCustomerName.CustomerCategoryID, " + "\r\n";
            queryString = queryString + "               VWListCustomerCategory.Description AS CustomerCategoryDescription, ListCustomerName.CustomerTypeID, " + "\r\n";
            queryString = queryString + "               VWListCustomerType.Description AS CustomerTypeDescription, VWListCustomerType.CustomerTypeID1, " + "\r\n";
            queryString = queryString + "               VWListCustomerType.Description1 AS CustomerTypeDescription1, VWListCustomerType.CustomerTypeID2, " + "\r\n";
            queryString = queryString + "               VWListCustomerType.Description2 AS CustomerTypeDescription2, VWListCustomerType.CustomerTypeID3, " + "\r\n";
            queryString = queryString + "               VWListCustomerType.Description3 AS CustomerTypeDescription3, VWListCustomerType.CustomerTypeID4, ListCustomerName.CustomerChannelID, " + "\r\n";
            queryString = queryString + "               VWListCustomerChannel.Description AS CustomerChannelDescription, VWListCustomerChannel.CustomerChannelID1, " + "\r\n";
            queryString = queryString + "               VWListCustomerChannel.Description1 AS CustomerChannelDescription1, VWListCustomerChannel.CustomerChannelID2, " + "\r\n";
            queryString = queryString + "               VWListCustomerChannel.Description2 AS CustomerChannelDescription2, VWListCustomerChannel.CustomerChannelID3, " + "\r\n";
            queryString = queryString + "               VWListCustomerChannel.Description3 AS CustomerChannelDescription3, VWListCustomerChannel.CustomerChannelID4, " + "\r\n";
            queryString = queryString + "               VWListCustomerType.Description4 AS CustomerTypeDescription4, VWListCustomerChannel.Description4 AS CustomerChannelDescription4, ListCustomerName.AddressNo, ListCustomerName.AddressAreaID, " + "\r\n";
            queryString = queryString + "               VWListAddressArea.Description AS AddressAreaDescription, VWListAddressArea.AreaCode, VWListAddressArea.AddressAreaID1, " + "\r\n";
            queryString = queryString + "               VWListAddressArea.Description1 AS AddressAreaDescription1, VWListAddressArea.AreaCode1, VWListAddressArea.AddressAreaID2, " + "\r\n";
            queryString = queryString + "               VWListAddressArea.Description2 AS AddressAreaDescription2, VWListAddressArea.AreaCode2, VWListAddressArea.AddressAreaID3, " + "\r\n";
            queryString = queryString + "               VWListAddressArea.Description3 AS AddressAreaDescription3, VWListAddressArea.AreaCode3, VWListAddressArea.AddressAreaID4, " + "\r\n";
            queryString = queryString + "               VWListAddressArea.Description4 AS AddressAreaDescription4, VWListAddressArea.AreaCode4, VWListAddressArea.AddressAreaID5, " + "\r\n";
            queryString = queryString + "               VWListAddressArea.Description5 AS AddressAreaDescription5, VWListAddressArea.AreaCode5, VWListAddressArea.AddressAreaID6, " + "\r\n";
            queryString = queryString + "               VWListAddressArea.Description6 AS AddressAreaDescription6, VWListAddressArea.AreaCode6, VWListAddressArea.AddressAreaID7, " + "\r\n";
            queryString = queryString + "               VWListAddressArea.Description7 AS AddressAreaDescription7, VWListAddressArea.AreaCode7, VWListAddressArea.Leafest, VWListAddressArea.AdministratorLevelID " + "\r\n";
            queryString = queryString + "   FROM        ListCustomerName INNER JOIN " + "\r\n";
            queryString = queryString + "               VWListCustomerCategory ON ListCustomerName.CustomerCategoryID = VWListCustomerCategory.CustomerCategoryID INNER JOIN " + "\r\n";
            queryString = queryString + "               VWListCustomerType ON ListCustomerName.CustomerTypeID = VWListCustomerType.CustomerTypeID INNER JOIN" + "\r\n";
            queryString = queryString + "               VWListCustomerChannel ON ListCustomerName.CustomerChannelID = VWListCustomerChannel.CustomerChannelID INNER JOIN" + "\r\n";
            queryString = queryString + "               VWListAddressArea ON ListCustomerName.AddressAreaID = VWListAddressArea.AddressAreaID" + "\r\n";

            SQLDatabase.CreateView("VWListCustomerName", queryString);
            //---************************VWListCustomerName.END
        }


        private static void RestoreVWListSalesmenCustomer()
        {
            string queryString = "";
            //---************************VWListSalesmenCustomer.BEGIN

            //D/S CUSTOMER DA GAN REP: BAO GOM CA REP CHUA GAN CUSTOMER (LEFT JOIN)

            queryString = "         SELECT      ListSalesmenName.Description AS SalesmenDescription, ListSalesmenName.DescriptionOfficial AS SalesmenDescriptionOfficial, ListSalesmenName.DescriptionLastName AS SalesmenDescriptionLastName, " + "\r\n";
            queryString = queryString + "               ListSalesmenName.IdentifyNumber, ListSalesmenName.SalesmenCommodityAll, " + "\r\n";
            queryString = queryString + "               ListSalesmenTitle.Description AS SalesmenTitleDescription, VWListSalesmenItemClass.ItemClassCC, " + "\r\n";
            queryString = queryString + "               VWListSalesmenItemClass.ItemClassNonCC, DERIVEDTBLSalesmenCustomer.Description AS CustomerDescription, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.DescriptionOfficial AS CustomerDescriptionOfficial, DERIVEDTBLSalesmenCustomer.VATCode, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.ContactName, DERIVEDTBLSalesmenCustomer.AttentionName, DERIVEDTBLSalesmenCustomer.Telephone, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.EMailAddress, DERIVEDTBLSalesmenCustomer.Other, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.CustomerCategoryDescription, DERIVEDTBLSalesmenCustomer.CustomerTypeDescription1, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.CustomerTypeDescription2, DERIVEDTBLSalesmenCustomer.CustomerTypeDescription3, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.CustomerTypeDescription4, DERIVEDTBLSalesmenCustomer.CustomerChannelDescription1, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.CustomerChannelDescription2, DERIVEDTBLSalesmenCustomer.CustomerChannelDescription3, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.CustomerChannelDescription4, DERIVEDTBLSalesmenCustomer.AddressNo, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.AddressAreaDescription1, DERIVEDTBLSalesmenCustomer.AddressAreaDescription2, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.AddressAreaDescription3, DERIVEDTBLSalesmenCustomer.AddressAreaDescription4, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.AddressAreaDescription5, DERIVEDTBLSalesmenCustomer.AddressAreaDescription6, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.AddressAreaDescription7, DERIVEDTBLSalesmenCustomer.SalesmenCustomerDescription, " + "\r\n";
            queryString = queryString + "               DERIVEDTBLSalesmenCustomer.SalesmenCustomerOther" + "\r\n";
            queryString = queryString + "   FROM        ListSalesmenName INNER JOIN " + "\r\n";
            queryString = queryString + "               ListSalesmenTitle ON ListSalesmenName.SalesmenTitleID = ListSalesmenTitle.SalesmenTitleID LEFT OUTER JOIN " + "\r\n";
            queryString = queryString + "               VWListSalesmenItemClass ON ListSalesmenName.SalesmenID = VWListSalesmenItemClass.SalesmenID LEFT OUTER JOIN " + "\r\n";
            queryString = queryString + "              (SELECT      ListSalesmenCustomer.SalesmenID, VWListCustomerName.Description, VWListCustomerName.DescriptionOfficial, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.DescriptionOriginal, VWListCustomerName.VATCode, VWListCustomerName.ContactName, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AttentionName, VWListCustomerName.Telephone, VWListCustomerName.EMailAddress, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.Other, VWListCustomerName.DependentOnly, VWListCustomerName.CustomerCategoryID, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerCategoryDescription, VWListCustomerName.CustomerTypeID, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerTypeDescription, VWListCustomerName.CustomerTypeID1, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerTypeDescription1, VWListCustomerName.CustomerTypeID2, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerTypeDescription2, VWListCustomerName.CustomerTypeID3, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerTypeDescription3, VWListCustomerName.CustomerTypeID4, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerTypeDescription4, VWListCustomerName.CustomerChannelID, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerChannelDescription, VWListCustomerName.CustomerChannelID1, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerChannelDescription1, VWListCustomerName.CustomerChannelID2, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerChannelDescription2, VWListCustomerName.CustomerChannelID3, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerChannelDescription3, VWListCustomerName.CustomerChannelID4, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.CustomerChannelDescription4, VWListCustomerName.AddressNo, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AddressAreaID, VWListCustomerName.AddressAreaDescription, VWListCustomerName.AreaCode, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AddressAreaID1, VWListCustomerName.AddressAreaDescription1, VWListCustomerName.AreaCode1, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AddressAreaID2, VWListCustomerName.AddressAreaDescription2, VWListCustomerName.AreaCode2, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AddressAreaID3, VWListCustomerName.AddressAreaDescription3, VWListCustomerName.AreaCode3, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AddressAreaID4, VWListCustomerName.AddressAreaDescription4, VWListCustomerName.AreaCode4, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AddressAreaID5, VWListCustomerName.AddressAreaDescription5, VWListCustomerName.AreaCode5, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AddressAreaID6, VWListCustomerName.AddressAreaDescription6, VWListCustomerName.AreaCode6, " + "\r\n";
            queryString = queryString + "                           VWListCustomerName.AddressAreaID7, VWListCustomerName.AddressAreaDescription7, VWListCustomerName.AreaCode7, " + "\r\n";
            queryString = queryString + "                           ListSalesmenCustomer.Description AS SalesmenCustomerDescription, " + "\r\n";
            queryString = queryString + "                           ListSalesmenCustomer.Other AS SalesmenCustomerOther" + "\r\n";
            queryString = queryString + "               FROM        ListSalesmenCustomer INNER JOIN" + "\r\n";
            queryString = queryString + "                           VWListCustomerName ON ListSalesmenCustomer.CustomerID = VWListCustomerName.CustomerID) DERIVEDTBLSalesmenCustomer ON ListSalesmenName.SalesmenID = DERIVEDTBLSalesmenCustomer.SalesmenID " + "\r\n";

            queryString = queryString + "   UNION ALL " + "\r\n";

            ////D/S CUSTOMER CHUA GAN REP (ONLY VWListCustomerName.DependentOnly = 0)
            queryString = queryString + "   SELECT      NULL AS SalesmenDescription, NULL AS SalesmenDescriptionOfficial, NULL AS SalesmenDescriptionLastName, NULL AS IdentifyNumber, NULL AS SalesmenCommodityAll, NULL AS SalesmenTitleDescription, " + "\r\n";
            queryString = queryString + "               NULL AS ItemClassCC, NULL AS ItemClassNonCC, Description AS CustomerDescription, " + "\r\n";
            queryString = queryString + "               DescriptionOfficial AS CustomerDescriptionOfficial, VATCode, ContactName, AttentionName, Telephone, EMailAddress, Other, " + "\r\n";
            queryString = queryString + "               CustomerCategoryDescription, CustomerTypeDescription1, CustomerTypeDescription2, CustomerTypeDescription3, CustomerTypeDescription4, " + "\r\n";
            queryString = queryString + "               CustomerChannelDescription1, CustomerChannelDescription2, CustomerChannelDescription3, CustomerChannelDescription4, " + "\r\n";
            queryString = queryString + "               AddressNo, AddressAreaDescription1, AddressAreaDescription2, AddressAreaDescription3, AddressAreaDescription4, AddressAreaDescription5, " + "\r\n";
            queryString = queryString + "               AddressAreaDescription6, AddressAreaDescription7, NULL AS SalesmenCustomerDescription, NULL AS SalesmenCustomerOther" + "\r\n";
            queryString = queryString + "   FROM        VWListCustomerName " + "\r\n";
            queryString = queryString + "   WHERE       VWListCustomerName.DependentOnly = 0 AND VWListCustomerName.CustomerID NOT IN (SELECT CustomerID FROM ListSalesmenCustomer)" + "\r\n";


            SQLDatabase.CreateView("VWListSalesmenCustomer", queryString);
            //---************************VWListSalesmenCustomer.END
        }


        private static void RestoreVWListSalesmenCategory()
        {
            string queryString = "";
            //---************************VWListSalesmenCategory.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               SalesmenCategoryID, Description, ISNULL((SELECT TOP 1 Description FROM ListSalesmenName WHERE ListSalesmenCategory.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorCode, ISNULL((SELECT TOP 1 DescriptionOfficial + ' ' + DescriptionLastName FROM ListSalesmenName WHERE ListSalesmenCategory.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorName, " + "\r\n";

            queryString = queryString + "               SalesmenCategoryID AS SalesmenCategoryID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListSalesmenCategory WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END
            //--------A2.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               SalesmenCategory2.SalesmenCategoryID, SalesmenCategory2.Description, ISNULL((SELECT TOP 1 Description FROM ListSalesmenName WHERE SalesmenCategory2.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorCode, ISNULL((SELECT TOP 1 DescriptionOfficial + ' ' + DescriptionLastName FROM ListSalesmenName WHERE SalesmenCategory2.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorName, " + "\r\n";

            queryString = queryString + "               SalesmenCategory1.SalesmenCategoryID AS SalesmenCategoryID1, SalesmenCategory1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               SalesmenCategory2.SalesmenCategoryID AS SalesmenCategoryID2, SalesmenCategory2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 1)) AS SalesmenCategory1 " + "\r\n";
            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 2)) AS SalesmenCategory2 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory1.SalesmenCategoryID = SalesmenCategory2.AncestorID " + "\r\n";
            //--------A2.END

            //--------A3.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               SalesmenCategory3.SalesmenCategoryID, SalesmenCategory3.Description, ISNULL((SELECT TOP 1 Description FROM ListSalesmenName WHERE SalesmenCategory3.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorCode, ISNULL((SELECT TOP 1 DescriptionOfficial + ' ' + DescriptionLastName FROM ListSalesmenName WHERE SalesmenCategory3.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorName, " + "\r\n";

            queryString = queryString + "               SalesmenCategory1.SalesmenCategoryID AS SalesmenCategoryID1, SalesmenCategory1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               SalesmenCategory2.SalesmenCategoryID AS SalesmenCategoryID2, SalesmenCategory2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               SalesmenCategory3.SalesmenCategoryID AS SalesmenCategoryID3, SalesmenCategory3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 1)) AS SalesmenCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 2)) AS SalesmenCategory2 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory1.SalesmenCategoryID = SalesmenCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 3)) AS SalesmenCategory3 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory2.SalesmenCategoryID = SalesmenCategory3.AncestorID " + "\r\n";
            //--------A3.END


            //--------A4.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               SalesmenCategory4.SalesmenCategoryID, SalesmenCategory4.Description, ISNULL((SELECT TOP 1 Description FROM ListSalesmenName WHERE SalesmenCategory4.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorCode, ISNULL((SELECT TOP 1 DescriptionOfficial + ' ' + DescriptionLastName FROM ListSalesmenName WHERE SalesmenCategory4.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorName, " + "\r\n";

            queryString = queryString + "               SalesmenCategory1.SalesmenCategoryID AS SalesmenCategoryID1, SalesmenCategory1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               SalesmenCategory2.SalesmenCategoryID AS SalesmenCategoryID2, SalesmenCategory2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               SalesmenCategory3.SalesmenCategoryID AS SalesmenCategoryID3, SalesmenCategory3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               SalesmenCategory4.SalesmenCategoryID AS SalesmenCategoryID4, SalesmenCategory4.Description AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 1)) AS SalesmenCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 2)) AS SalesmenCategory2 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory1.SalesmenCategoryID = SalesmenCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 3)) AS SalesmenCategory3 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory2.SalesmenCategoryID = SalesmenCategory3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 4)) AS SalesmenCategory4 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory3.SalesmenCategoryID = SalesmenCategory4.AncestorID " + "\r\n";
            //--------A4.END

            //--------A5.BEGIN
            queryString = queryString + "   UNION ALL   " + "\r\n";

            queryString = queryString + "   SELECT      " + "\r\n";
            queryString = queryString + "               SalesmenCategory5.SalesmenCategoryID, SalesmenCategory5.Description, ISNULL((SELECT TOP 1 Description FROM ListSalesmenName WHERE SalesmenCategory5.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorCode, ISNULL((SELECT TOP 1 DescriptionOfficial + ' ' + DescriptionLastName FROM ListSalesmenName WHERE SalesmenCategory5.SalesmenID = ListSalesmenName.SalesmenID), '') AS SupervisorName, " + "\r\n";

            queryString = queryString + "               SalesmenCategory1.SalesmenCategoryID AS SalesmenCategoryID1, SalesmenCategory1.Description AS Description1, " + "\r\n";
            queryString = queryString + "               SalesmenCategory2.SalesmenCategoryID AS SalesmenCategoryID2, SalesmenCategory2.Description AS Description2, " + "\r\n";
            queryString = queryString + "               SalesmenCategory3.SalesmenCategoryID AS SalesmenCategoryID3, SalesmenCategory3.Description AS Description3, " + "\r\n";
            queryString = queryString + "               SalesmenCategory4.SalesmenCategoryID AS SalesmenCategoryID4, SalesmenCategory4.Description AS Description4, " + "\r\n";
            queryString = queryString + "               SalesmenCategory5.SalesmenCategoryID AS SalesmenCategoryID5, SalesmenCategory5.Description AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS SalesmenCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        " + "\r\n";

            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 1)) AS SalesmenCategory1 " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 2)) AS SalesmenCategory2 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory1.SalesmenCategoryID = SalesmenCategory2.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 3)) AS SalesmenCategory3 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory2.SalesmenCategoryID = SalesmenCategory3.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 4)) AS SalesmenCategory4 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory3.SalesmenCategoryID = SalesmenCategory4.AncestorID " + "\r\n";

            queryString = queryString + "               INNER JOIN " + "\r\n";
            queryString = queryString + "               (SELECT     SalesmenCategoryID, Description, SalesmenID,  AncestorID FROM ListSalesmenCategory WHERE (HierarchyLevel = 5)) AS SalesmenCategory5 " + "\r\n";
            queryString = queryString + "               ON SalesmenCategory4.SalesmenCategoryID = SalesmenCategory5.AncestorID " + "\r\n";
            //--------A5.END         

            SQLDatabase.CreateView("VWListSalesmenCategory", queryString);
            //---************************VWListSalesmenCategory.END
        }


        private static void RestoreVWListDistributorCategory()
        {
            string queryString = "";
            //---************************VWListDistributorCategory.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               DistributorCategoryID, Description, " + "\r\n";

            queryString = queryString + "               DistributorCategoryID AS DistributorCategoryID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS DistributorCategoryID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS DistributorCategoryID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS DistributorCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS DistributorCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS DistributorCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS DistributorCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS DistributorCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS DistributorCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListDistributorCategory WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END

            SQLDatabase.CreateView("VWListDistributorCategory", queryString);
            //---************************VWListDistributorCategory.END

        }


        private static void RestoreVWListStaffCategory()
        {
            string queryString = "";

            //---************************VWListStaffCategory.BEGIN
            //--------A1.BIGIN
            queryString = "   SELECT      " + "\r\n";
            queryString = queryString + "               StaffCategoryID, Description, " + "\r\n";

            queryString = queryString + "               StaffCategoryID AS StaffCategoryID1, Description AS Description1, " + "\r\n";
            queryString = queryString + "               0 AS StaffCategoryID2, '' AS Description2, " + "\r\n";
            queryString = queryString + "               0 AS StaffCategoryID3, '' AS Description3, " + "\r\n";
            queryString = queryString + "               0 AS StaffCategoryID4, '' AS Description4, " + "\r\n";
            queryString = queryString + "               0 AS StaffCategoryID5, '' AS Description5, " + "\r\n";
            queryString = queryString + "               0 AS StaffCategoryID6, '' AS Description6, " + "\r\n";
            queryString = queryString + "               0 AS StaffCategoryID7, '' AS Description7, " + "\r\n";
            queryString = queryString + "               0 AS StaffCategoryID8, '' AS Description8, " + "\r\n";
            queryString = queryString + "               0 AS StaffCategoryID9, '' AS Description9 " + "\r\n";

            queryString = queryString + "   FROM        ListStaffCategory WHERE (HierarchyLevel = 1) " + "\r\n";

            //--------A1.END            
            SQLDatabase.CreateView("VWListStaffCategory", queryString);
            //---************************VWListStaffCategory.END
        }
    }
}
